<?php

namespace Libs\aws;

/**
 * class Message for Send Email from AWS SES
 * 
 * @access  public
 * @author TinNT1 (tinnt1@vitalify.asia)
 * @copyright Copyright@2014 Vitaliify Asia. All Rights Reserved.
 * @version 1.0
 * create date : 20/3/2014
 * modify date : 21/3/2014
 */
class Message {
	
	private $_fromEmail;
	private $_toEmails, $_cc, $_bcc, $_replyTo;
	private $_body;
	private $_subject;
	private $_bodyType;
	private $_charset;
	private $_returnPath = '';
	private $_attachments = array();
	
	
	/**
	 * Init Message Entity
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sFromEmail
	 * @param array 	$aryToEmail
	 * @param string 	$sSubject
	 * @param string 	$sBody
	 * @param string 	$sCharset
	 * @param string 	$sBodyType		text|html
	 */
	function __construct($sFromEmail = '', $aryToEmail = array(), $sSubject = '', $sBody = '', $sCharset = 'utf-8', $sBodyType = "text") {
		$this->_fromEmail 	= $sFromEmail;
		$this->_toEmails 	= $aryToEmail;
		$this->_body 		= $sBody;
		$this->_subject 	= $sSubject;
		$this->_bodyType 	= strtolower($sBodyType);
		$this->_charset 	= strtolower($sCharset);
		$this->_cc 			= array();
		$this->_bcc 		= array();
		$this->_replyTo 	= array();
	}
	
	/**
	 * set from email
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sFromEmail
	 */
	public function setFromEmail($sFromEmail) {
		$this->_fromEmail = $sFromEmail;
	}
	
	/**
	 * set to emails
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param array 	$aryToEmail
	 */
	public function setToEmail($aryToEmail) {
		$this->_toEmails = $aryToEmail;
	}
	
	/**
	 * set to cc
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param array 	$aryCC
	 */
	public function setCC($aryCC) {
		$this->_cc = $aryCC;
	}
	
	/**
	 * set to bcc
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param array 	$aryBCC
	 */
	public function setBCC($aryBCC) {
		$this->_bcc = $aryBCC;
	}
	
	/**
	 * set to reply to
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param array 	$aryReplyTo
	 */
	public function setReplyTo($aryReplyTo) {
		$this->_replyTo = $aryReplyTo;
	}
	
	/**
	 * set body message
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sBody
	 */
	public function setBody($sBody){
		$this->_body = $sBody;
	}
	
	/**
	 * set subject
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sSubject
	 */
	public function setSubject($sSubject){
		$this->_subject = $sSubject;
	}
	
	/**
	 * set type body
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sBodyType
	 */
	public function setBodyType($sBodyType){
		$this->_bodyType = strtolower($sBodyType);
	}
	
	/**
	 * set charset code for body
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string 	$sCharset
	 */
	public function setCharsetCode($sCharset){
		$this->_charset = strtolower($sCharset);
	}
	
	/**
	 * set return path (email) using for error, server mail will send mail error to this
	 * 
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string $sReturnPath
	 */
	public function setReturnPath($sReturnPath){
		$this -> _returnPath = $sReturnPath;
	}
	
	/**
	 * Add email attachment by directly passing the content
	 *
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string $name The name of the file attachment as it will appear in the email
	 * @param string $data The contents of the attachment file
	 * @param string $mimeType Specify custom MIME type
	 * @param string $contentId Content ID of the attachment for inclusion in the mail message
	 * @return void
	 * 
	 */
	public function addAttachmentFromData($name, $data, $mimeType = 'application/octet-stream', $contentId = null) {
		$this->_attachments[$name] = array(
					'name' => $name,
					'mimeType' => $mimeType,
					'data' => $data,
					'contentId' => $contentId,
		);
	}
	
	/**
	 * Add email attachment by passing file path
	 *
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @param string $name 			The name of the file attachment as it will appear in the email
	 * @param string $path 			Path to the attachment file
	 * @param string $mimeType 		Specify custom MIME type
	 * @param string $contentId 	Content ID of the attachment for inclusion in the mail message
	 * @return boolean 				Status of the operation
	 * 
	 */
	public function addAttachmentFromFile($name, $path, $mimeType = 'application/octet-stream', $contentId = null) {
		if (file_exists($path) && is_file($path) && is_readable($path)) {
			$this->_attachments[$name] = array(
						'name' => $name,
						'mimeType' => $mimeType,
						'data' => file_get_contents($path),
						'contentId' => $contentId,
			);
			return true;
		}
		return false;
	}
	
	/**
	 * Get boolean is a message raw (have attachments file)
	 *
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * return boolean   
	 */
	public function isMessageRaw(){
		if($this->_attachments){
			return true;
		}
		return false;
	}
	
	/**
	 * get message package build for send mail
	 * 
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * return array $messageContent
	 */
	public function getMessagePackage(){
		$messageContent = array('Source' 		=> $this->_fromEmail,
								'Destination' 	=> array('ToAddresses' 	=> $this->_toEmails));
								
		if($this->_bodyType == 'html'){
			$messageContent['Message'] = array('Body' => array('Html' => array('Data' => $this->_body,
																				'Charset' => $this->_charset)),
												'Subject' 	=> array('Data' => $this->_subject,
																	'Charset' => $this->_charset));
		} else {
			$messageContent['Message'] = array('Body' => array('Text' => array('Data' => $this->_body,
																				'Charset' => $this->_charset)),
												'Subject' 	=> array('Data' => $this->_subject,
																	'Charset' => $this->_charset));
		}
		
		if(!empty($this -> _returnPath)){
			$messageContent['ReturnPath'] = $this -> _returnPath;
		}
		return $messageContent;
	}	
	
	/**
	 * Get the raw mail message
	 *
	 * @author TinNT1 (tinnt1@vitalify.asia)
	 * @return array 	$aryReturn 
	 */
	public function getRawMessagePackage()
	{
		$boundary = uniqid(rand(), true);
		$raw_message = "To: " . join(', ', $this->_toEmails) . "\n";
		$raw_message .= "From: " . $this->_fromEmail . "\n";
		if (!empty($this->_cc)) {
			$raw_message .= "CC: " . join(', ', $this->_cc) . "\n";
		}
		if (!empty($this->_bcc)) {
			$raw_message .= "BCC: " . join(', ', $this->_bcc) . "\n";
		}
		if (!empty($this->_returnPath)) {
			$raw_message .= "Return-Path: " . $this->_returnPath . "\n";
		}
		
		if($this->_subject != null && strlen($this->_subject) > 0) {
			if(empty($this->_charset)) {
				$raw_message .= 'Subject: ' . $this->_subject . "\n";
			} else {
				$raw_message .= 'Subject: =?' . $this->_charset . '?B?' . base64_encode($this->_subject) . '?=' . "\n";
			}
		}
		$raw_message .= 'MIME-Version: 1.0' . "\n";
		$raw_message .= 'Content-type: Multipart/Mixed; boundary="' . $boundary . '"' . "\n";
		$raw_message .= "\n--{$boundary}\n";
		$raw_message .= 'Content-type: Multipart/Alternative; boundary="alt-' . $boundary . '"' . "\n";
		
		if($this->_body != null && strlen($this->_body) > 0) {
			$charset = empty($this->_charset) ? '' : "; charset=\"{$this->_charset}\"";
			$raw_message .= "\n--alt-{$boundary}\n";
			if($this->_bodyType == 'html') {
				$raw_message .= 'Content-Type: text/html' . $charset . "\n\n";
			} else {
				$raw_message .= 'Content-Type: text/plain' . $charset . "\n\n";
			}
			$raw_message .= $this->_body . "\n";
		}
		
		$raw_message .= "\n--alt-{$boundary}--\n";
		
		foreach($this->_attachments as $attachment) {
			$raw_message .= "\n--{$boundary}\n";
			$raw_message .= 'Content-Type: ' . $attachment['mimeType'] . '; name="' . $attachment['name'] . '"' . "\n";
			$raw_message .= 'Content-Disposition: attachment' . "\n";
			if(!empty($attachment['contentId'])) {
				$raw_message .= 'Content-ID: ' . $attachment['contentId'] . '' . "\n";
			}
			$raw_message .= 'Content-Transfer-Encoding: base64' . "\n";
			$raw_message .= "\n" . chunk_split(base64_encode($attachment['data']), 76, "\n") . "\n";
		}
		
		$raw_message .= "\n--{$boundary}--\n";			
		$aryReturn = array('RawMessage' => array('Data' => base64_encode($raw_message)));
		return $aryReturn;
	}
	
}
